<?php

class categoriesActions extends sfActions {
    
    public function executeIndex(sfWebRequest $request){
        $categories = array();
        $c = new Criteria();
        
        $c->clear();
        $c->add(CategoryPeer::ENABLED, 1);
        $c->addAscendingOrderByColumn(CategoryPeer::SEQUENCE);
        $categories = CategoryPeer::doSelect($c);
        $this->categories = $categories;
    }
    
    public function executeNew(sfWebRequest $request){
        $parent_id = $request->getParameter('parent_id');
        $c = new Criteria();
        
        $c->clear();
        $c->add(CategoryPeer::ID, $parent_id);
        $c->add(CategoryPeer::ENABLED, 1);
        $this->category = CategoryPeer::doSelectOne($c);
    }
    
    public function executeNew_save(sfWebRequest $request){
        $hdParentCategoryId = $request->getParameter('hdParentCategoryId');
        $txtName = $request->getParameter('txtName');
        $txtUrl = $request->getParameter('txtUrl');
        $txtSecuencia = $request->getParameter('txtSecuencia');
        
        $category = new Category();
        $category->setName($txtName);
        $category->setUrl($txtUrl);
        $category->setSequence($txtSecuencia);
        $category->setParentId($hdParentCategoryId);
        $category->setEnabled(1);
        $category->save();
        
        $this->getUser()->setFlash('success_notification','Categoría creada satisfactoriamente');
        $this->redirect('@categories');
    }
    
    public function executeEdit(sfWebRequest $request){
        $category_id = $request->getParameter('category_id');
        $c = new Criteria();
        
        $c->clear();
        $c->add(CategoryPeer::ID, $category_id);
        $c->add(CategoryPeer::ENABLED, 1);
        $this->category = CategoryPeer::doSelectOne($c);
    }
    
    public function executeEdit_save(sfWebRequest $request){
        $hdCategoryId = $request->getParameter('hdCategoryId');
        $txtName = $request->getParameter('txtName');
        $txtUrl = $request->getParameter('txtUrl');
        $txtSecuencia = $request->getParameter('txtSecuencia');
        $c = new Criteria();
        
        $c->clear();
        $c->add(CategoryPeer::ID, $hdCategoryId);
        $c->add(CategoryPeer::ENABLED, 1);
        $category = CategoryPeer::doSelectOne($c);
        
        if(is_object($category)){
            $category->setName($txtName);
            $category->setUrl($txtUrl);
            $category->setSequence($txtSecuencia);
            $category->setEnabled(1);
            $category->save();
        }
        
        $this->getUser()->setFlash('success_notification','Categoría modificada satisfactoriamente');
        $this->redirect('@categories');
    }
    
    public function executeDisable(sfWebRequest $request){
        $category_id = $request->getParameter('category_id');
        $c = new Criteria();
        
        $c->clear();
        $c->add(CategoryPeer::ID, $category_id);
        $c->add(CategoryPeer::ENABLED, 1);
        $category = CategoryPeer::doSelectOne($c);
        
        if(is_object($category)){
            $category->setEnabled(0);
            $category->save();
        }
        
        $this->getUser()->setFlash('success_notification','Categoría dehsabilitada satisfactoriamente');
        $this->redirect('@categories');
    }
    
}